package com.roger.ats.init.cases;

import android.content.Context;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager;
import android.os.RemoteException;
import android.os.SystemClock;
import android.support.test.InstrumentationRegistry;
import android.support.test.uiautomator.UiCollection;
import android.support.test.uiautomator.UiObject;
import android.support.test.uiautomator.UiScrollable;
import android.support.test.uiautomator.UiSelector;
import android.util.Log;
import com.roger.ats.init.util.CfgUtils;
import com.roger.ats.init.util.LogUtils;
import com.roger.uia.helper.UiBase;
import com.roger.uia.helper.manager.Package;
import com.roger.uia.helper.manager.Telephony;
import com.roger.uia.helper.manager.Wifi;
import com.roger.uia.helper.util.StrUtils;
import java.util.List;

import static com.roger.uia.helper.UiBase.mDevice;
import static com.roger.uia.helper.UiBase.waitTime;

public class SettingsInit {

    private static final String TAG = "Test" + SettingsInit.class.getSimpleName();
    private static CfgUtils cfgUtils = new CfgUtils();
    private static LogUtils logUtils = new LogUtils();
    static String hot_seat = "com.tblenovo.launcher:id/hotseat";
    private static UiScrollable list = new UiScrollable(new UiSelector().resourceId(StrUtils.settingList));

    public static void connWifi(){
        mDevice.pressHome();
        String ifConn = cfgUtils.getCfg("是否连接=");
        String wifiName = cfgUtils.getCfg("wifi名称=");
        String wifiPwd = cfgUtils.getCfg("wifi密码=");
        UiObject menuText = mDevice.findObject(new UiSelector().textMatches(wifiName));
        String success="(?i)"+wifiName+",Connected.*|"+wifiName+",已连接.*";
        UiScrollable secMenuList = new UiScrollable(new UiSelector().resourceId(StrUtils.settingList));
        logUtils.printLog("==start connect wifi==");
        for(int i=0;i<3;i++){
            try {
                if(ifConn.equals("true")){
                    if(!Wifi.wifi().getWifiSaved().contains(wifiName)){
                        //removeWifiSaved();
                        enterWLAN();
                        for (int j = 0; j < 3; j++) {
                            secMenuList.scrollIntoView(menuText);
                            if (menuText.exists()) {
                                menuText.clickAndWaitForNewWindow();
                                break;
                            }
                        }
                        if (!wifiPwd.equals("null")) {
                            UiBase.waitObjCheck(UiBase.objSelect[3], StrUtils.password, 5);
                            UiBase.inputTxtToObj(UiBase.objSelect[3], StrUtils.password, wifiPwd);
                            UiBase.waitClickObj(UiBase.objSelect[2], StrUtils.connect, 10);
                        }
                        UiBase.waitObjCheck(UiBase.objSelect[6],success,30);
                        logUtils.printLog("connect wifi success : " + wifiName);
                        break;
                    }else{
                        logUtils.printLog("already connect to : " + wifiName);
                        break;
                    }
                }
            }catch (Throwable e){
                logUtils.printLog("connect wifi fail : " + wifiName);
            }finally {
                mDevice.pressHome();
            }
        }
    }

    public static void setMobileData(){
        mDevice.pressHome();
        String arg = cfgUtils.getCfg("移动网络=");
        try{
            logUtils.printLog("==start set mobile data==");
            if(Telephony.tel().hasSIM()){
                mDevice.openQuickSettings();
                int y = UiBase.getObjY(UiBase.objSelect[3],"com.android.systemui:id/tile_page");
                if(!UiBase.existObj(UiBase.objSelect[6],"移动数据.*")){
                    mDevice.swipe(StrUtils.SCREEN_W -20 ,y,20,y,50);
                }
                if(!UiBase.getTxtFromObj(UiBase.objSelect[6],"移动数据.*").equals(arg)){
                    UiBase.clickObj(UiBase.objSelect[6],"移动数据.*");
                }
                UiBase.checkPoint("check status",UiBase.getTxtFromObj(UiBase.objSelect[6],"移动数据.*").equals(arg));
                logUtils.printLog("set mobile data success");
            }else{
                logUtils.printLog("no sim");
            }
        }catch (Throwable e){
            logUtils.printLog("set mobile data fail");
        }finally {
            mDevice.pressHome();
        }
    }

    public static void setBTName(){
        mDevice.pressHome();
        String name = cfgUtils.getCfg("蓝牙设备名称=");
        logUtils.printLog("==start set BT name==");
        try {
            UiBase.openApp(StrUtils.settingPac);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
            UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[2],"已连接的设备|已关联的设备");
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,3);
            UiBase.waitClickIfObjExist(UiBase.objSelect[2],"连接偏好设置",3);
            UiBase.waitClickObj(UiBase.objSelect[2],"蓝牙",3);
            UiBase.waitClickIfObjExist(UiBase.objSelect[2],"关闭",3);
            UiBase.waitObj(UiBase.objSelect[2],"设备名称",10);
            if(!UiBase.existObj(UiBase.objSelect[0],name)){
                UiBase.clickObj(UiBase.objSelect[2],"设备名称");
                UiBase.waitObj(UiBase.objSelect[3],"com.android.settings:id/edittext",3);
                UiBase.inputTxtToObj(UiBase.objSelect[3],"com.android.settings:id/edittext",name);
                UiBase.waitClickObj(UiBase.objSelect[2],"重命名",10);
            }
            UiBase.checkPoint("check navigation bar style",UiBase.existObj(UiBase.objSelect[0],name));
            logUtils.printLog("set BT name success : " + name);
        } catch (Throwable e) {
            logUtils.printLog("set BT name fail : " + name);
        }finally {
            mDevice.pressHome();
        }
    }

    public static void setBrightness(){
        mDevice.pressHome();
        logUtils.printLog("==start set brightness level==");
        try {
            mDevice.executeShellCommand("settings put system screen_brightness 255");
            UiBase.openApp(StrUtils.settingPac);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
            UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[2],StrUtils.settingDisplay);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,3);
            UiBase.clickIfObjExist(UiBase.objSelect[2],StrUtils.advanced);
            UiBase.checkPoint("check status",UiBase.existObj(UiBase.objSelect[0],"100%"));
            logUtils.printLog("set brightness level success");
        } catch (Throwable e) {
            logUtils.printLog("set brightness level fail");
        }finally {
            mDevice.pressHome();
        }
    }

    public static void setAutoBrightness(){
        mDevice.pressHome();
        String arg = cfgUtils.getCfg("自动调节亮度=");
        logUtils.printLog("==start set auto brightness==");
        try {
            UiBase.openApp(StrUtils.settingPac);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
            UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[2],StrUtils.settingDisplay);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,3);
            UiBase.clickIfObjExist(UiBase.objSelect[2],StrUtils.advanced);
            UiBase.scrollReidClickObj(StrUtils.settingList,UiBase.objSelect[2],"自动调节亮度");
            if(!UiBase.getObj(UiBase.objSelect[3],"android:id/switch_widget").getText().equals(arg)){
                UiBase.clickObj(UiBase.objSelect[3],"android:id/switch_widget");
            }
            UiBase.checkPoint("check status",UiBase.getObj(UiBase.objSelect[3],"android:id/switch_widget").getText().equals(arg));
            logUtils.printLog("set auto brightness success");
        } catch (Throwable e) {
            logUtils.printLog("set auto brightness fail");
        }finally {
            mDevice.pressHome();
        }
    }

    public static void setDesktop(){
        mDevice.pressHome();
        String style = cfgUtils.getCfg("桌面风格=");
        logUtils.printLog("==start set desktop style==");
        try {
            UiBase.openApp(StrUtils.settingPac);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
            UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[2],StrUtils.settingDisplay);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,3);
            UiBase.clickIfObjExist(UiBase.objSelect[2],StrUtils.advanced);
            UiBase.scrollReidClickObj(StrUtils.settingList,UiBase.objSelect[2],"桌面风格");
            UiBase.waitClickObj(UiBase.objSelect[2],style,3);
            logUtils.printLog("set desktop style success : " + style);
        } catch (Throwable e) {
            logUtils.printLog("set desktop style fail : " + style);
        }finally {
            UiBase.waitObj(UiBase.objSelect[3],hot_seat,10);
            mDevice.pressHome();
        }
    }

    public static void setNavigation(){
        mDevice.pressHome();
        String style = cfgUtils.getCfg("导航栏=");
        String btn = "";
        if(style.equals("经典模式")) btn = "com.android.settings:id/radiobutton_classic_nav";
        if(style.equals("工作模式")) btn = "com.android.settings:id/radiobutton_work_nav";
        if(style.equals("手势模式")) btn = "com.android.settings:id/textView_gesture_nav";
        logUtils.printLog("==start set navigation bar==");
        try {
            UiBase.openApp(StrUtils.settingPac);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
            UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[2],StrUtils.settingDisplay);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,3);
            UiBase.clickIfObjExist(UiBase.objSelect[2],StrUtils.advanced);
            UiBase.scrollReidClickObj(StrUtils.settingList,UiBase.objSelect[2],"导航栏|导航栏模式");
            if(UiBase.existObj(UiBase.objSelect[2],"默认")){
                UiBase.clickObj(UiBase.objSelect[2],"默认");
            }else{
                UiBase.waitClickObj(UiBase.objSelect[3],btn,3);
                UiBase.checkPoint("check navigation bar style",UiBase.objChecked(UiBase.objSelect[3],btn));
            }
            logUtils.printLog("set navigation bar success : " + style);
        } catch (Throwable e) {
            logUtils.printLog("set navigation bar fail : " + style);
        }finally {
            mDevice.pressHome();
        }
    }

    public static void changeWallpaper(){
        mDevice.pressHome();
        String pic= cfgUtils.getCfg("更改背景图=");
        logUtils.printLog("==start set wallpaper==");
        try {
            UiBase.openApp(StrUtils.settingPac);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
            UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[2],StrUtils.settingDisplay);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,3);
            UiBase.clickIfObjExist(UiBase.objSelect[2],StrUtils.advanced);
            UiBase.scrollReidClickObj(StrUtils.settingList,UiBase.objSelect[2],"壁纸");
            UiBase.waitObj(UiBase.objSelect[2],"选择壁纸来源",3);
            UiBase.scrollReidClickObj(StrUtils.settingList,UiBase.objSelect[2],"壁纸");
            UiBase.waitClickObj(UiBase.objSelect[2],"我的照片",3);
            UiBase.waitClickObj(UiBase.objSelect[4],"显示根目录",3);
            UiBase.waitClickObj(UiBase.objSelect[2],"图片",3);
            UiBase.waitClickObj(UiBase.objSelect[2],"DCIM",3);
            UiBase.waitClickObj(UiBase.objSelect[2],pic,5);
            UiBase.waitClickObj(UiBase.objSelect[2],"设置壁纸",5);
            UiBase.waitClickIfObjExist(UiBase.objSelect[2],"主屏幕",5);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,5);
            logUtils.printLog("set wallpaper success");
        } catch (Throwable e) {
            logUtils.printLog("set wallpaper fail");
        }finally {
            mDevice.pressHome();
        }
    }

    public static void setAutoRotate(){
        mDevice.pressHome();
        String arg = cfgUtils.getCfg("自动旋转屏幕=");
        logUtils.printLog("==start set auto rotate screen==");
        try {
            UiBase.openApp(StrUtils.settingPac);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
            UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[2],StrUtils.settingDisplay);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,3);
            UiBase.clickIfObjExist(UiBase.objSelect[2],StrUtils.advanced);
            list.scrollIntoView(UiBase.getObj(UiBase.objSelect[0],"自动旋转屏幕"));
            if(!UiBase.getObjSameLine(list,UiBase.getObj(UiBase.objSelect[0],"自动旋转屏幕"),UiBase.getObj(UiBase.objSelect[3],"android:id/switch_widget")).getText().equals(arg)){
                UiBase.clickObj(UiBase.objSelect[0],"自动旋转屏幕");
            }
            UiBase.checkPoint("check status",UiBase.getObjSameLine(list,UiBase.getObj(UiBase.objSelect[0],"自动旋转屏幕"),UiBase.getObj(UiBase.objSelect[3],"android:id/switch_widget")).getText().equals(arg));
            logUtils.printLog("set auto rotate screen success");
        } catch (Throwable e) {
            logUtils.printLog("set auto rotate screen fail");
        }finally {
            mDevice.pressHome();
        }
    }

    public static void setBattery(){
        mDevice.pressHome();
        String arg1 = cfgUtils.getCfg("待机智能省电=");
        String arg2 = cfgUtils.getCfg("电池保护模式=");
        logUtils.printLog("==start set battery==");
        try {
            UiBase.openApp(StrUtils.settingPac);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
            UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[2],StrUtils.settingBattery);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,3);
            list.scrollIntoView(UiBase.getObj(UiBase.objSelect[2],"待机智能省电"));
            UiObject obj1 = UiBase.getObjSameLine(new UiCollection(new UiSelector().resourceId(StrUtils.settingList)),UiBase.getObj(UiBase.objSelect[2],"待机智能省电"),UiBase.getObj(UiBase.objSelect[3],"android:id/switch_widget"));
            if(!obj1.getText().equals(arg1))  obj1.clickAndWaitForNewWindow();
            list.scrollIntoView(UiBase.getObj(UiBase.objSelect[2],"电池保护模式"));
            UiObject objParent = UiBase.getObj(UiBase.objSelect[2],"电池保护模式").getFromParent(new UiSelector());
            UiObject obj2 = UiBase.getObjSameLine(new UiCollection(new UiSelector().resourceId(StrUtils.settingList)),objParent,UiBase.getObj(UiBase.objSelect[3],"android:id/switch_widget"));
            if(!obj2.getText().equals(arg2))  obj2.clickAndWaitForNewWindow();
            UiBase.checkPoint("check status",obj1.getText().equals(arg1) && obj2.getText().equals(arg2));
            logUtils.printLog("set battery success");
        } catch (Throwable e) {
            logUtils.printLog("set battery fail");
        }finally {
            mDevice.pressHome();
        }
    }

    public static void setLock(){
        mDevice.pressHome();
        String arg = cfgUtils.getCfg("屏幕锁定=");
        String pin = cfgUtils.getCfg("PIN=");
        logUtils.printLog("==start set screen lock==");
        try {
            UiBase.openApp(StrUtils.settingPac);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
            UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[2],StrUtils.settingSecurity);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,3);
            UiBase.scrollReidClickObj(StrUtils.settingList,UiBase.objSelect[2],StrUtils.ScreenLock);
            UiBase.waitClickObj(UiBase.objSelect[0],arg,3);
            if(arg.equals("PIN 码")){
                UiBase.waitClickObj(UiBase.objSelect[0],"否",3);
                UiBase.waitObjCheck(UiBase.objSelect[3],"com.android.settings:id/password_entry",3);
                UiBase.inputTxtToObj(UiBase.objSelect[3],"com.android.settings:id/password_entry",pin);
                UiBase.waitClickObj(UiBase.objSelect[2],"下一步",3);
                UiBase.waitObjCheck(UiBase.objSelect[3],"com.android.settings:id/password_entry",3);
                UiBase.inputTxtToObj(UiBase.objSelect[3],"com.android.settings:id/password_entry",pin);
                UiBase.waitClickObj(UiBase.objSelect[2],"确认|确定",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"完成",5);
            }
            UiBase.checkPoint("check status",UiBase.existObj(UiBase.objSelect[0],arg));
            logUtils.printLog("set screen lock success : " + arg);
        } catch (Throwable e) {
            logUtils.printLog("set screen lock fail : " + arg);
        }finally {
            mDevice.pressHome();
        }
    }

    public static void setMultiUser(){
        mDevice.pressHome();
        String arg = cfgUtils.getCfg("一般用户=");
        String arg1 = cfgUtils.getCfg("学生用户=");
        logUtils.printLog("==start set multi user==");
        try {
            UiBase.openApp(StrUtils.settingPac);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
            UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[2],"用户和帐号");
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,3);
            UiBase.scrollReidClickObj(StrUtils.settingList,UiBase.objSelect[2],"多用户|用户");
            if(!UiBase.existObj(UiBase.objSelect[0],arg)){
                UiBase.waitClickObj(UiBase.objSelect[0],"添加用户",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"一般用户",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"继续",3);
                UiBase.waitObj(UiBase.objSelect[0],"姓名",3);
                UiBase.inputTxtToObj(UiBase.objSelect[0],"姓名",arg);
                UiBase.waitClickObj(UiBase.objSelect[0],"下一步",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"关闭",20);
            }
            if(!Package.pac().isAppInstalled("com.android.chrome") && !UiBase.existObj(UiBase.objSelect[0],arg1)){
                UiBase.waitClickObj(UiBase.objSelect[0],"添加用户",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"学生用户",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"继续",3);
                UiBase.waitObj(UiBase.objSelect[0],"姓名",3);
                UiBase.inputTxtToObj(UiBase.objSelect[0],"姓名",arg1);
                UiBase.clickObj(UiBase.objSelect[0],"生日");
                UiBase.waitClickObj(UiBase.objSelect[0],"确定",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"年级",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"学龄前",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"确定",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"地区",3);
                UiBase.scrollReidClickObj("com.android.wantjoin.settings:id/region_recycler_view",UiBase.objSelect[2],"北京");
                UiBase.waitClickObj(UiBase.objSelect[0],"确定",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"下一步",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"完成",3);
                UiBase.waitClickObj(UiBase.objSelect[0],"关闭",20);
                UiBase.checkPoint("check status",UiBase.existObj(UiBase.objSelect[0],arg1));
                logUtils.printLog("set multi user success : " + arg1);
            }
            UiBase.checkPoint("check status",UiBase.existObj(UiBase.objSelect[0],arg));
            logUtils.printLog("set multi user success : " + arg);
        } catch (Throwable e) {
            logUtils.printLog("set multi user fail : " + arg);
        }finally {
            mDevice.pressHome();
        }
    }

    public static void setKeyBoard(){
        mDevice.pressHome();
        String arg = cfgUtils.getCfg("按键边框=");
        logUtils.printLog("==start set keyboard==");
        if(Package.pac().isAppInstalled("com.google.android.inputmethod.latin")){
            for (int i=0;i<3;i++){
                try {
                    UiBase.openApp(StrUtils.settingPac);
                    UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
                    UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[0],"系统");
                    UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,3);
                    UiBase.scrollReidClickObj(StrUtils.settingList,UiBase.objSelect[2],"语言和输入法");
                    UiBase.waitClickObj(UiBase.objSelect[0],"虚拟键盘",5);
                    UiBase.waitClickObj(UiBase.objSelect[0],"Gboard",5);
                    UiBase.waitClickObj(UiBase.objSelect[0],"主题背景",5);
                    UiBase.waitClickObj(UiBase.objSelect[4],"Google 浅色主题",5);
                    UiBase.waitObjCheck(UiBase.objSelect[2],"按键边框.*",3);
                    if(!UiBase.existObj(UiBase.objSelect[0],"按键边框 "+ arg))
                        UiBase.clickObj(UiBase.objSelect[2],"按键边框.*");
                    UiBase.checkPoint("check status",UiBase.existObj(UiBase.objSelect[0],"按键边框 "+ arg));
                    UiBase.clickObj(UiBase.objSelect[0],"应用");
                    logUtils.printLog("set keyboard success : " + arg);
                    break;
                } catch (Throwable e) {
                    logUtils.printLog("set keyboard fail : " + arg);
                }finally {
                    mDevice.pressHome();
                }
            }
        }else {
            logUtils.printLog("not find Gboard" );
        }
    }

    public static void setLenovoUpdate(){
        mDevice.pressHome();
        String arg = cfgUtils.getCfg("联想功能更新=");
        logUtils.printLog("==start set lenovo update==");
        for (int i=0;i<3;i++){
            try {
                UiBase.openApp(StrUtils.settingPac);
                UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
                UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[0],"系统");
                UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingList,3);
                UiBase.scrollReidClickObj(StrUtils.settingList,UiBase.objSelect[2],"联想功能更新");
                if(UiBase.getTxtFromObj(UiBase.objSelect[3],"com.tblenovo.whatsnewclient:id/sw_control").equals("开启"))
                    UiBase.clickObj(UiBase.objSelect[3],"com.tblenovo.whatsnewclient:id/sw_control");
                UiBase.checkPoint("check status",UiBase.getTxtFromObj(UiBase.objSelect[3],"com.tblenovo.whatsnewclient:id/sw_control").equals(arg));
                logUtils.printLog("set lenovo update success : " + arg);
                break;
            } catch (Throwable e) {
                logUtils.printLog("set lenovo update fail : " + arg);
            }finally {
                mDevice.pressHome();
            }
        }
    }

    public static void setWellBeing(){
        mDevice.pressHome();
        logUtils.printLog("==start set digital WellBeing==");
        for (int i=0;i<3;i++){
            try {
                UiBase.openApp(StrUtils.settingPac);
                UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
                UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[2],"数字健康.*");
                UiBase.waitClickIfObjExist(UiBase.objSelect[2],"显示您的数据",3);
                UiBase.waitObj(UiBase.objSelect[3],"com.google.android.apps.wellbeing:id/settings_scroll_view",3);
                if(UiBase.existObj(UiBase.objSelect[3],"com.google.android.apps.wellbeing:id/settings_scroll_view"))
                    UiBase.scrollReidClickObj("com.google.android.apps.wellbeing:id/settings_scroll_view",UiBase.objSelect[2],"放松模式");
                else
                    UiBase.scrollReidClickObj("com.google.android.apps.wellbeing:id/settings_item_list",UiBase.objSelect[2],"放松模式");
                UiBase.waitClickObj(UiBase.objSelect[2],"跳过",10);
                UiBase.checkPoint("check status",UiBase.existObj(UiBase.objSelect[2],"开始时间"));
                logUtils.printLog("set digital WellBeing success");
                break;
            } catch (Throwable e) {
                logUtils.printLog("set digital WellBeing fail");
            }finally {
                mDevice.pressHome();
                if(mDevice.isNaturalOrientation()) {
                    try {
                        mDevice.setOrientationLeft();
                    } catch (RemoteException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    private static void enterWLAN() throws Throwable {
        if (UiBase.existObj(UiBase.objSelect[2],StrUtils.wifi) && UiBase.existObj(UiBase.objSelect[3],StrUtils.frictionIcon)) {
            SystemClock.sleep(waitTime);
        } else {
            mDevice.pressHome();
            UiBase.openApp(StrUtils.settingPac);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.settingDashBoard,10);
            UiBase.scrollReidClickObj(StrUtils.settingDashBoard,UiBase.objSelect[2],StrUtils.settingNetwork);
            UiBase.waitClickObj(UiBase.objSelect[2],StrUtils.wifi,5);
            UiBase.waitObj(UiBase.objSelect[3],StrUtils.switch_widget,5);
            if (UiBase.getTxtFromObj(UiBase.objSelect[3],StrUtils.switch_widget).matches(StrUtils.off)) {
                UiBase.clickObj(UiBase.objSelect[3],StrUtils.switch_widget);
                SystemClock.sleep(waitTime * 10);
            }
            SystemClock.sleep(waitTime * 5);
        }
    }

    private static void removeWifiSaved(){
        try{
            WifiManager wifiManager = (WifiManager) InstrumentationRegistry.getTargetContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
            List<WifiConfiguration> wifiConfigs = wifiManager.getConfiguredNetworks();
            if(!String.valueOf(wifiConfigs).equals("[]")){
                enterWLAN();
                UiScrollable secMenuList = new UiScrollable(new UiSelector().resourceId(StrUtils.settingList));
                for(int j=0;j<3;j++){
                    secMenuList.scrollIntoView(UiBase.getObj(UiBase.objSelect[2],StrUtils.savedWifi));
                    if(UiBase.existObj(UiBase.objSelect[2],StrUtils.savedWifi)) {
                        UiBase.clickObj(UiBase.objSelect[2],StrUtils.savedWifi);
                        for (WifiConfiguration wifiConfig : wifiConfigs) {
                            String name = String.valueOf(wifiConfig.SSID.replaceAll("\"",""));
                            Log.d(TAG,"remove wifi : "+ name);
                            if (UiBase.existObj(UiBase.objSelect[0],name)){
                                UiBase.clickObj(UiBase.objSelect[0],name);
                                UiBase.waitClickObj(UiBase.objSelect[2],StrUtils.forget,3);
                            }
                        }
                        break;
                    }else {
                        Log.d(TAG,"find saved wifi fail");
                    }
                }
            }else{
                Log.d(TAG,"no saved wifi");
            }
        }catch (Throwable e){
            Log.d(TAG,"remove saved wifi fail");
            e.printStackTrace();
        }finally {
            mDevice.pressBack();
        }
    }


}
